package com.mano.mall.gateway.handler.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mano.commons.web.utils.HttpRequestUtil;
import com.mano.mall.api.dto.gatewayhandler.output.BlackListOutputDTO;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.constants.Constants;
import com.mano.mall.gateway.feign.BlackListServiceFeign;
import com.mano.mall.gateway.handler.GatewayHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

/**
 * 黑名单拦截判断
 * 
 */
@Component
@Slf4j
public class BlacklistHandler extends BaseHandler implements GatewayHandler {

	@Autowired
	private BlackListServiceFeign blackListServiceFeign;

	@Override
	public void service(RequestContext ctx, HttpServletRequest request, HttpServletResponse response) {
		log.info("<<<<流程1判断 黑名单拦截>>>>>>>>");
		// 3.黑名单限制blacklistMapper
		String ipAddr = HttpRequestUtil.getIpAddr(request);
		BaseResponse<BlackListOutputDTO> baseResponse = blackListServiceFeign.findBlacklist(ipAddr);

		if(isSuccessData(baseResponse)){
			resultError(Constants.HTTP_NOT_AUTH,ctx, "ip:" + ipAddr + ",Insufficient access rights");
			return;
		}

		// 执行下一个流程
		gatewayHandler.service(ctx, request, response);

	}

}
